package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;

/**我的订单
 * @author Administrator
 *
 */
public class Shop4 extends Activity {

	private List<Map<String, Object>> list_gwc = new ArrayList<Map<String, Object>>();
	private ListView lv_gwc;
	private TextView  tv_je;
	MyApplication app;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.shop4_main);
		MyApplication myApplication;
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		findView();

		getData();

	}

	private void findView() {

		lv_gwc = (ListView) this.findViewById(R.id.listView);

		app = (MyApplication) this.getApplication();

		list_gwc = new ArrayList<Map<String, Object>>();
		tv_je = (TextView) this.findViewById(R.id.tv_je);

	}

	private void getData() {
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData() {
		
		try {
			Message msg = mUIHandler.obtainMessage();
			Bundle bundle = new Bundle();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("drzh", app.getAccount()));

			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.SPDDXXFIND, ls);

			if (json != null && !json.equals("")) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (new JSONArray(json.getString("spInfo")).length()>0) {
						// 获取订单信息
						JSONArray jsonArr = new JSONArray(
								json.getString("spInfo"));
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							Map<String, Object> map = new HashMap<String, Object>();
							js = jsonArr.getJSONObject(i);
							
							map.put("name", js.getString("spname"));
							map.put("price", js.getString("price"));
							map.put("dw", js.getString("dw"));
							map.put("imgpath", js.getString("imgpath"));
							map.put("num", js.getString("num"));
							String strId = js.getString("id");
							
							// 获取图片
							List<NameValuePair> ls1 = new ArrayList<NameValuePair>();
							ls1.add(new BasicNameValuePair("id", strId));
							Bitmap bitmap = HTTPGetTool.getTool()
									.queryByteForPost(
											URLUtils.HOST + URLUtils.SPDDXXPIC,
											ls1);
							map.put("pic", bitmap);
							list_gwc.add(map);
						}

						bundle.putInt("type", 0);
						bundle.putString("msg", "");
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}

				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}

			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			mUIHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 捕捉异常
			Bundle bundle = new Bundle();
			bundle.putInt("type", -1);
			bundle.putString("msg", e.toString());
			Message msg = mUIHandler.obtainMessage();
			msg.setData(bundle);
			mUIHandler.sendMessage(msg);
		}
		
	}

	private Handler mUIHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			int type = msg.getData().getInt("type");
			switch (type) {
			case -1:
				// proDialog.dismiss();
				Toast.makeText(Shop4.this, msg.getData().getString("msg"),
						Toast.LENGTH_SHORT).show();
				break;
			case 0: {
				setData();
				break;
			}

			}

		}

	};

	private void setData() {
		MyAdapter myAdapter = new MyAdapter(this);
		if (list_gwc != null) {
			myAdapter.setList(list_gwc);

			lv_gwc.setAdapter(myAdapter);
			
			Map<String, Object> newmap = new HashMap<String, Object>();
			double je = 0.0;
			for (int i = 0; i < list_gwc.size(); i++) {
				newmap = list_gwc.get(i);
				je += Double.valueOf(newmap.get("price").toString())*Integer.valueOf(newmap.get("num").toString());
				
			}
			tv_je.setText(je+"");
		}

	}

	private class MyAdapter extends BaseAdapter {
		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		public MyAdapter(Context context) {
			mLayoutInflater = (LayoutInflater) context
					.getSystemService(LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(R.layout.shop4_sub, null);
				holder = new ViewHolder();
				holder.gridview_imageview = (ImageView) convertView
						.findViewById(R.id.iv_img);
				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				holder.tv_price = (TextView) convertView
						.findViewById(R.id.tv_price);
				holder.tv_dw = (TextView) convertView
						.findViewById(R.id.tv_dw);
				
				holder.tv_sl = (TextView) convertView.findViewById(R.id.tv_sl);
				
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.gridview_imageview.setImageBitmap((Bitmap) list
					.get(position).get("pic"));
			holder.tv_name.setText(list.get(position).get("name").toString());
			holder.tv_price.setText(list.get(position).get("price").toString());
			holder.tv_dw.setText(list.get(position).get("dw").toString());
			holder.tv_sl.setText("X"+list.get(position).get("num").toString());
			
			return convertView;
		}
	}

	public class ViewHolder {
		private ImageView gridview_imageview;
		private TextView tv_name;
		private TextView tv_price;
		private TextView tv_dw;
		private TextView tv_sl;
	}

}
